package cn.ranko.shiro.controller;

import cn.ranko.bean.Datagrid;
import cn.ranko.bean.Pagination;
import cn.ranko.shiro.bean.authority.Message;
import cn.ranko.shiro.constants.ApplicationCode;
import cn.ranko.shiro.model.SysClass;
import cn.ranko.shiro.model.SysUser;
import cn.ranko.shiro.model.SysUserRole;
import cn.ranko.shiro.realm.ShiroDbRealm;
import cn.ranko.shiro.service.SysClassService;
import cn.ranko.shiro.service.SysRoleService;
import cn.ranko.shiro.service.SysUserService;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by user on 2017/8/25.
 */
@Controller
@RequestMapping("/shiro/user")
public class UserController {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysRoleService sysRoleService;
    
    @Autowired
	SysClassService sysClassService;

    @RequiresPermissions("user")
    @RequestMapping("/index.action")
    public String index(ModelMap modelMap){
        return "user/index";
    }

    @RequiresPermissions("user")
    @RequestMapping("/loadList.action")
    @ResponseBody
    public Datagrid loadList(Integer page,Integer rows){
        Pagination pagination = new Pagination();
        pagination.setPage(page);
        pagination.setRows(rows);
        return sysUserService.loadList(pagination);
    }

    @RequiresPermissions(value = {"user_add","user_delete"},logical = Logical.OR)
    @RequestMapping("/showedit.action")
    public String showedit(Integer user_id,ModelMap modelMap){
        if(user_id==0){
            //添加
        	 List<SysClass> querySysClassAll = sysClassService.getQuerySysClassAll();
        	 modelMap.addAttribute("sysClassList",querySysClassAll);
        }else{
            //修改
            SysUser sysUser = sysUserService.selectByUserId(user_id);
            modelMap.addAttribute("sysUser",sysUser);

            List<SysUserRole> sysUserRoles = sysRoleService.getSysUserRoleByUserId(user_id);
            modelMap.addAttribute("sysUserRoles",sysUserRoles);
        }
        return "user/showedit";
    }
    /**
     * 跳转到初始化密码页面
     * @param user_id
     * @param modelMap
     * @return
     */
    @RequiresPermissions(value = {"user_initPassword"},logical = Logical.OR)
    @RequestMapping("/user_initPassword.action")
    public String user_initPassword(Integer user_id,ModelMap modelMap){
    	if(!StringUtils.isEmpty(user_id)){
    		SysUser selectByUser = sysUserService.selectByUserId(user_id);
    		modelMap.addAttribute("sysUser",selectByUser);
    	}
        return "user/user_initPassword";
    }
    
    /**
     *  初始化密码
     * @param user
     * @param roleIds
     * @return
     */
    @RequiresPermissions(value = {"user_initPassword"},logical = Logical.OR)
    @RequestMapping("/user_initPassword_save.action")
    @ResponseBody
    public Integer user_initPassword_save(SysUser user,ModelMap modelMap){
        int affect = 0;
        if(!StringUtils.isEmpty(user.getUserId())){
        	SysUser selectByUser = sysUserService.selectByUserId(user.getUserId());
        	if(null != sysUserService){
        		affect = sysUserService.updatePassword(selectByUser, "123456");
        	}
        }
        return affect;
    }
    
    @RequiresPermissions(value = {"user_add","user_delete"},logical = Logical.OR)
    @RequestMapping("/save.action")
    @ResponseBody
    public String save(SysUser user,String roleIds,String classId){
        int affect = sysUserService.save(user,roleIds,classId);
        if(affect>0){
            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(),ApplicationCode.SUCCESS.getMessage()));
        }else {
            return JSON.toJSONString(new Message(ApplicationCode.ERROR.getCode(),ApplicationCode.ERROR.getMessage()));
        }
    }

    @RequiresPermissions("user_delete")
    @RequestMapping("/delete.action")
    @ResponseBody
    public String delete(Integer user_id){
        int affect = sysUserService.delete(user_id);
        if(affect>0){
            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(),ApplicationCode.SUCCESS.getMessage()));
        }else {
            return JSON.toJSONString(new Message(ApplicationCode.ERROR.getCode(),ApplicationCode.ERROR.getMessage()));
        }
    }

    @RequiresPermissions("user_resetpassword")
    @RequestMapping("/updatePassword.action")
    @ResponseBody
    public String updatePassword(String password){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(ShiroDbRealm.SESSION_USER_KEY);
        int affect = sysUserService.updatePassword(sysUser,password);
        if(affect>0){
            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(),ApplicationCode.SUCCESS.getMessage()));
        }else {
            return JSON.toJSONString(new Message(ApplicationCode.ERROR.getCode(),ApplicationCode.ERROR.getMessage()));
        }
    }

}
